package com.uwantsoft.util.validator;

import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author LeiPeichen.
 */
@Component(value = "isIntegerValidator")
public class IsPositiveIntegerValidator implements ConstraintValidator<IsPositiveInteger,String> {

    @Override
    public void initialize(IsPositiveInteger constraintAnnotation) {}

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        Pattern pattern = Pattern.compile("^\\d+$");
        Matcher matcher = pattern.matcher(value);
        if(matcher.find()){
            return true;
        }else{
            return false;
        }
    }
}
